package impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import bean.Student;
import dao.StudentMapper;
import service.StudentManager;
import util.MybatisSessionFactory;

public class StudentManagerImpl implements StudentManager {
	private StudentMapper studentMapper = null;
	private SqlSession sqlsession = null;
	private Logger logger = LoggerFactory.getLogger(StudentManagerImpl.class);
	private void opsession(){
		//sqlsession = MybatisSessionFactory.openSession(ture);可以设置自动提交事务fetch呵呵哒
		sqlsession = MybatisSessionFactory.openSession();
		studentMapper = sqlsession.getMapper(StudentMapper.class);
	}
	public int insert(Student student) {
		opsession();
		try{
			int result =  studentMapper.insert(student);
			sqlsession.commit();
			return result;
		}finally{
			sqlsession.close();
		}
	}

	public int delete(Student student) {
		opsession();
		try{
			int result = studentMapper.delete(student);
			sqlsession.commit();
			return result;
		}finally{
			sqlsession.close();
		}
	}

	public int update(Student student) {
		opsession();
		try{
			int result = studentMapper.update(student);
			sqlsession.commit();
			return result;
		}finally{
			sqlsession.close();
		}
	}

	public Student get(String no) {
		opsession();
		Student student = null;
		try{
			student = studentMapper.get(no);
			System.out.println(student.getName());
			sqlsession.commit();
		}finally{
			sqlsession.close();
		}		
		return student;
	}
	public int insertAll(Student[] studentArr) {
		opsession();
		try{
			int result = studentMapper.insertAllByArr(studentArr);
			sqlsession.commit();
			return result;
		}finally{
			sqlsession.close();
		}
	}
	public int insertAll(List<Student> studentList) {
		opsession();
		try{
			int result = studentMapper.insertAllByList(studentList);
			sqlsession.commit();
			return result;
		}finally{
			sqlsession.close();
		}
	}
	public int insertAll(Map<String, Student> studentMap) {
		try{
			opsession();
			int result = studentMapper.insertAllByMap(studentMap);
			sqlsession.commit();
			return result;
		}finally{
			sqlsession.close();
		}
	}
	public List<Student> getStudentList(String name) {
		try{
			opsession();
			List<Student> studentList = studentMapper.getStudentList(name);
			return studentList;
		}finally{
			sqlsession.close();
		}
	}

}